local tianmixianjing = fk.CreateSkill{
    name = "ym1__tianmixianjing",
    dynamic_desc = function(self, player)
        if player:getMark("@@ym1__daimashikong") ~= 0 then
            return "ym1__tianmixianjing_fix"
        else
            return ""
        end
    end,
}
Fk:loadTranslationTable{
    ["ym1__tianmixianjing"] = "甜蜜陷阱",
    [":ym1__tianmixianjing"] = "轮次开始时，若场上无“甜蜜陷阱”角色，你须选择一名其他角色。每回合你前三次获得牌后，可令其摸一张牌，或弃置你与其区域内各一张牌。",
    [":ym1__tianmixianjing_fix"] = "轮次开始时，若场上无“甜蜜陷阱”角色，你须选择一名其他角色。每回合你前三次获得牌后，可令其摸一张牌，或弃置其区域内各一张牌。",
    ["@ym1__tianmixianjing"] = "甜蜜恋爱",
    ["@ym1__tianmixianjing_true"] = "甜蜜陷阱",
    ["@@ym1__tianmixianjing"] = "锟斤拷 烫烫烫",
    ["#ym1__tianmixianjing-choose"] = "甜蜜陷阱：选择一名其他角色",
    ["#ym1__tianmixianjing-choice"] = "甜蜜陷阱：令%dest摸一张牌，或弃置你与其区域内各一张牌",
    ["#ym1__tianmixianjing2-choice"] = "甜蜜陷阱：令%dest摸一张牌，或弃置其区域内各一张牌",
    ["ym1__tianmixianjing_draw"] = "摸牌",
    ["ym1__tianmixianjing_discard"] = "弃牌",
    ["#ym1__tianmixianjing-discardchoose"] = "甜蜜陷阱：弃置你和其区域内各一张牌",
    ["#ym1__tianmixianjing2-discardchoose"] = "甜蜜陷阱：弃置其区域内各一张牌",

    ["$ym1__tianmixianjing1"] = "我相信，有了你，有了玩家的帮助，一定能成功的。",
    ["$ym1__tianmixianjing2"] = "我是你在这里能找到的最友善的人了，亲爱的。",
    ["$ym1__tianmixianjing3"] = "那我们现在来把事情捋捋清楚吧？",
    ["$ym1__tianmixianjing4"] = "她是个杀人狂，我们得阻止她。",
    ["$ym1__tianmixianjing5"] = "可能是一些故障，漏洞，异常...",

    ["$ym1__tianmixianjing_ym1__MiSide21"] = "但是，玩家，不管怎么说，我还是很喜欢你的！",
    ["$ym1__tianmixianjing_ym1__MiSide22"] = "我是说，你本可以留下来和我在一起的...",
    ["$ym1__tianmixianjing_ym1__MiSide23"] = "我们很快会再见的！",
    ["$ym1__tianmixianjing_ym1__MiSide24"] = "你为什么要逃跑？",
    ["$ym1__tianmixianjing_ym1__MiSide25"] = "你为什么要从我身边逃走？",
}
tianmixianjing:addEffect(fk.RoundStart,{
    mute = true,
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(tianmixianjing.name) then
            return player:getMark(tianmixianjing.name) == 0
        end
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        if #room:getOtherPlayers(player, false) == 0 then return false end
        local to = room:askToChoosePlayers(player,{
            targets = room:getOtherPlayers(player, false),
            max_num = 1,
            min_num = 1,
            prompt = "#ym1__tianmixianjing-choose",
            skill_name = tianmixianjing.name,
            cancelable = false,
            no_indicate = true,
        })
        if #to > 0 then
            event:setCostData(self, {tos = to})
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:notifySkillInvoked(player, tianmixianjing.name, "special")
        local to = event:getCostData(self).tos[1]
        room:setPlayerMark(player, tianmixianjing.name, to.id)
        if player:getMark("@@ym1__daimashikong") ~= 0 then
            player:broadcastSkillInvoke(tianmixianjing.name,1)
            room:setPlayerMark(to, "@@ym1__tianmixianjing", 1)
        else
            player:broadcastSkillInvoke(tianmixianjing.name,1)
            room:setPlayerMark(to, "@ym1__tianmixianjing", player.general)
        end
    end,
})
tianmixianjing:addEffect(fk.AfterCardsMove,{
    mute = true,
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(tianmixianjing.name) then
            local num = #player.room.logic:getEventsOfScope(GameEvent.MoveCards, 4, function(e)
                for _, move in ipairs(e.data) do
                    if move.to == player.id and move.toArea == Card.PlayerHand then
                        return true
                    end
                end
            end, Player.HistoryTurn)
            if num > 3 or player:getMark(tianmixianjing.name) == 0 then return false end
            for _, move in ipairs(data) do
                if move.to == player and move.toArea == Card.PlayerHand then
                    return true
                end
            end
        end
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local tp = player.room:getPlayerById(player:getMark(tianmixianjing.name))
        local choices = {"ym1__tianmixianjing_draw"}
        if ( #player:getCardIds("hej") > 0  or player:getMark("@@ym1__daimashikong") ~= 0 ) and #tp:getCardIds("hej") > 0 then
            table.insert(choices,"ym1__tianmixianjing_discard")
        end
        table.insert(choices,"Cancel")
        local prompt = "#ym1__tianmixianjing-choice::"..player:getMark(tianmixianjing.name)
        if player:getMark("@@ym1__daimashikong") ~= 0 then
            prompt = "#ym1__tianmixianjing2-choice::"..player:getMark(tianmixianjing.name)
        end
        local choice = room:askToChoice(player,{
            choices = choices,
            skill_name = tianmixianjing.name,
            prompt = prompt,
            all_choices = {"ym1__tianmixianjing_draw", "ym1__tianmixianjing_discard", "Cancel"},
        })
        if choice ~= "Cancel" then
            self.cost_data = choice
            return true
        else
            return false
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local tp = player.room:getPlayerById(player:getMark(tianmixianjing.name))
        if self.cost_data == "ym1__tianmixianjing_draw" then
            room:notifySkillInvoked(player, tianmixianjing.name, "drawcard")
            tp:drawCards(1, tianmixianjing.name)
            if player:getMark("@@ym1__daimashikong") == 0 then
                room:setPlayerMark(tp, "@ym1__tianmixianjing_true", 0)
                room:setPlayerMark(tp, "@ym1__tianmixianjing", player.general)
                player:broadcastSkillInvoke(tianmixianjing.name,table.random({2,3}))
            else
                player:broadcastSkillInvoke(tianmixianjing.name,table.random({2,3}))
            end
        else
            local card_data = {}
            if player:getMark("@@ym1__daimashikong") == 0 then
                room:setPlayerMark(tp, "@ym1__tianmixianjing", 0)
                room:setPlayerMark(tp, "@ym1__tianmixianjing_true", player.general)
                card_data = {{tp.general,{}},{"你",{}}}
                player:broadcastSkillInvoke(tianmixianjing.name,table.random({4,5}))
            else
                player:broadcastSkillInvoke(tianmixianjing.name,table.random({4,5}))
            end
            if tp:getHandcardNum() > 0 then
                local handcards = {}
                for _ = 1, tp:getHandcardNum() do
                    table.insert(handcards, -1) -- 手牌不可见
                end
                if player:getMark("@@ym1__daimashikong") ~= 0 then
                    table.insert(card_data, {"$Hand_opposite", handcards})
                else
                    table.insertTable(card_data[1][2], handcards)
                end
            end
            local areas = { {"$Equip", Player.Equip}, {"$Judge", Player.Judge} }
            for _, v in ipairs(areas) do
                local area = v[2]
                if #tp.player_cards[area] > 0 then
                    if player:getMark("@@ym1__daimashikong") ~= 0 then
                        table.insert(card_data, {v[1] .. "_opposite", tp:getCardIds(area)})
                    else
                        table.insertTable(card_data[1][2], tp:getCardIds(area))
                    end
                end
            end
            table.insert(areas, 1, { "$Hand", Player.Hand})
            if player:getMark("@@ym1__daimashikong") == 0 then
                for _, v in ipairs(areas) do
                    local area = v[2]
                    if #player.player_cards[area] > 0 then
                        table.insertTable(card_data[2][2], player:getCardIds(area))
                    end
                end
            end

            local ret = room:askToPoxi(player,{
                poxi_type = "ym1__tianmixianjing_poxi",
                data = card_data,
                cancelable = false,
            })
            local new_ret = table.filter(ret, function(id) return id ~= -1 end)
            local hand_num = #ret - #new_ret
            if hand_num > 0 then
                table.insertTable(new_ret, table.random(tp:getCardIds(Player.Hand), hand_num))
            end
            local moveInfos = {}
            local cards1 = {}
            for i = #new_ret, 1, -1 do
                if room:getCardOwner(new_ret[i]) == player then
                    table.insert(cards1, new_ret[i])
                    table.remove(new_ret, i)
                end
            end
            if #cards1 > 0 then
                table.insert(moveInfos, {
                    from = player.id,
                    ids = cards1,
                    toArea = Card.DiscardPile,
                    moveReason = fk.ReasonDiscard,
                    proposer = player.id,
                    skillName = tianmixianjing.name,
                })
            end
            if #new_ret > 0 then
                table.insert(moveInfos, {
                    from = tp.id,
                    ids = new_ret,
                    toArea = Card.DiscardPile,
                    moveReason = fk.ReasonDiscard,
                    proposer = player.id,
                    skillName = tianmixianjing.name,
                })
            end
            room:notifySkillInvoked(player, tianmixianjing.name, "control")
            room:moveCards(table.unpack(moveInfos))
        end
    end,
})
tianmixianjing:addEffect(fk.BuryVictim,{
    can_refresh = function(self, event, target, player, data)
        return player:getMark(tianmixianjing.name) == target.id
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        room:setPlayerMark(player, tianmixianjing.name, 0)
    end,
})
Fk:addPoxiMethod{
    name = "ym1__tianmixianjing_poxi",
    card_filter = function(to_select, selected, data)
        for _, areas in ipairs(data) do
            if #table.filter(selected,function (id)
                return table.contains(areas[2],id)
            end)>0 and table.contains(areas[2],to_select) then
               return false 
            end
        end
        return #selected < #data
    end,
    feasible = function(selected, data)
      return data and #selected == #data
    end,
    prompt = function (data)
        if data and data[2] and data[2][1] == "你" then
            return "#ym1__tianmixianjing-discardchoose"
        else
            return "#ym1__tianmixianjing2-discardchoose"
        end
    end,
    default_choice = function (data)
        if not data then return {} end
        local cids = table.map(data,function (v)
            return v[2][1]
        end)
        return cids
    end,
}
return tianmixianjing